package com.example.qihuibao.widget.view

import android.app.TimePickerDialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.*
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.qihuibao.R
import com.example.qihuibao.bean.*
import com.example.qihuibao.constant.Global
import com.example.qihuibao.enum.MealStatus
import com.example.qihuibao.enum.OrderMode
import com.example.qihuibao.enum.Status
import com.example.qihuibao.module.main.MainActivity
import com.example.qihuibao.utils.EnumUtils
import com.example.qihuibao.utils.extension.toast
import java.util.*

/**
 *     author : cwwei
 *     e-mail : 494545699@qq.com
 *     date   : 2022/08/13
 *     desc   :订餐明细
 */
class OrderInfoLayout(ctx:Context) : FrameLayout(ctx) {
    private lateinit var activity: MainActivity
    private lateinit var root: View
    private val orderList = mutableListOf<RspQueryOrderInfoBean.Order>()

    init {
        if (ctx is MainActivity) {
            activity = ctx
        }
        root = LayoutInflater.from(ctx).inflate(R.layout.layout_order, this, true)
        root.setOnClickListener {  }//禁止点击事件穿透
        initView()
    }

    private fun initView() {
        root.findViewById<View>(R.id.iv_back).setOnClickListener { (parent as ViewGroup).removeView(this) }
        root.findViewById<TextView>(R.id.tv_title).text = "订餐明细"
        orderList.clear()
        orderList.add(RspQueryOrderInfoBean.Order())
        orderList.add(RspQueryOrderInfoBean.Order(
            "",
            "000001",
            "cww",
            "1",
            "01",
            "",
            "1234567890",
        "20240101",
        "01",
        "09",
        "1234567890202401010109",
        "20240101010101",
        1,
        1000,
        0,
        1000,
            UseCount = 0,
            OrderMode = "套餐",
            DeliverFlag = "不配送",
            OrderType = "现场取餐")
        )
        queryOrderInfo(pageNo = 1)
    }

    private fun queryOrderInfo(MealDateBegin:String=Global.dateFormat.format(Date()), MealDateEnd:String=Global.dateFormat.format(Date()), MealID:String="0", MealSetID:String="0", Status:String="0", pageNo:Int) {
        activity?.getMainViewModel().queryOrderInfo(ReqQueryOrderInfoBean(PageNo = pageNo)).observe(activity) {
            if (it.RetCode == "0") {
                if (!it.OrderList.isNullOrEmpty()) {
                    orderList.addAll(it.OrderList!!)
                }
                if (it.HasNext == "1") {
                    queryOrderInfo(MealDateBegin, MealDateEnd, MealID, MealSetID, Status, pageNo)
                } else {
                    //show data
                    val rv_order = root.findViewById<CommonRecyclerView>(R.id.rv_order)
                    rv_order.init(
                        R.layout.item_order,
                        LinearLayoutManager(context, RecyclerView.VERTICAL, false),
                        orderList,
                        { parent, pos ->
                            if (orderList.isNotEmpty()) {
                                if (pos > 0) {
                                    parent.findViewById<TextView>(R.id.tv_EmpID).text = orderList[pos].EmpID
                                    parent.findViewById<TextView>(R.id.tv_EmpName).text = orderList[pos].EmpName
                                    parent.findViewById<TextView>(R.id.tv_PayID).text = orderList[pos].PayID
                                    parent.findViewById<TextView>(R.id.tv_MealDate).text = orderList[pos].MealDate
                                    parent.findViewById<TextView>(R.id.tv_MealID).text = orderList[pos].MealID
                                    parent.findViewById<TextView>(R.id.tv_MealSetID).text = orderList[pos].MealSetID
                                    parent.findViewById<TextView>(R.id.tv_TranID).text = orderList[pos].TranID
                                    parent.findViewById<TextView>(R.id.tv_TranTime).text = orderList[pos].TranTime
                                    parent.findViewById<TextView>(R.id.tv_Count).text = "${orderList[pos].Count}"
                                }
                            }
                        },
                        {pos ->
//                            showDetail(employList[pos])
                        }
                    )
                }
            }else {
                activity?.toast("查询失败")
            }
        }
    }

}